ReplaceText
ReplaceText Replace indicated text with specified substitution
#include <Script.h> Script Manager
short ReplaceText( baseText, substitutionText, key );
Handle baseText ; a handle to the text to be searched
Handle substitutionText ; a handle to the new text
Str15 key ; string to substitute
returns a short; a positive value indicating the number of
substitutions performed or a negative value
indicating an error
ReplaceText searches the text specified by the baseText parameter for
instances of the key string and replaces each instance with the text indicated
by the substitutionText parameter. The key parameter contains a string to be
used as the substitution marker. Although the substitution text may contain the
key string, the text is inserted verbatim into the base text, and no recursive
substitution occurs.
Returns: a positive value indicating the number of substitutions performed or
a negative value indicating an error. The constant noErr is returned
if there is no error or no substitutions performed.The following are
general Memory Manager errors. When ReplaceText returns
these errors by using the following constants, they have these
specific meanings:
Result codes
(special meanings of Memory Manager errors returned by ReplaceText)
memFullErr (108) SetHandleSize fails on baseText
nilHandleErr (109) GetHandleSize fails on baseText or substitutionText
memWZErr (-111) GetHandleSize fails on baseText or substitutionText

Notes: ReplaceText may move memory.
The code example below uses the ReplaceText and TruncText functions and
assumes that you have Str255 strings containing base text and substitution
text and that you want the result to fit in a specified number of pixels.
// Substituting and truncating text
// Assuming inclusion of
#include
#include < string.h>
# define maxInt 32767;
void SubAndTruncText (void);
void DoError (OSErr myErr);
void SubAndTruncText ()
{
Str255 baseString;
Str255 subsString;
Handle baseHandle;
Handle subsHandle;
Str15 keyStr;
long sizeL;
short myWidth;
short length;
short result;
OSErr myErr;
strcpy ((char *) baseString, (char *)
"\pabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
// insert into baseString...
strcpy ((char *) subsString, (char *) "\pKILROY WAS HERE");
strcpy ((char *) keyStr, (char *)"\pmnop");
// ...in place of this sequence
myWidth = 500; [TOKEN:12079] ...and truncate with this width
sizeL = baseString[0];
myErr = PtrToHand(&baseString[1], & baseHandle, sizeL);
if (myErr)
DoError(myErr);
sizeL = subsString[0];
myErr = PtrToHand(&subsString[1], &subsHandle, sizeL);
if (myErr)
DoError(myErr);
result = ReplaceText( baseHandle, subsHandle, keyStr);
if (result < 0)
DoError( result);
sizeL = GetHandleSize( baseHandle);
if(MemErr)
DoError(MemErr);
length = sizeL;
HLock( baseHandle);
if (MemErr)
DoError(MemErr); [TOKEN:12079] oops, a Mem Mgr error
result = TruncText(myWidth, (* baseHandle), & length, smTruncEnd);
if (result < 0)
DoError( result);
DrawText((* baseHandle), 0, length);
HUnlock( baseHandle);
if (MemErr)
DoError(myErr); [TOKEN:12079] oops, a Mem Mgr error
}